'''
Created on May 12, 2011

@author: bruk
'''
import Actor
import pygame #@UnresolvedImport
from pygame.locals import *



def PlayerControl(actor,events):

    for event in events:
        keysdown = pygame.key.get_pressed()
    
        if (event.type == KEYUP):
        #check if any other of the animation keys are pressed,
            if (not keysdown[K_UP] and not keysdown[K_DOWN] and not keysdown[K_LEFT] and not keysdown[K_RIGHT]):
                actor.State = 'IDLE'
                actor.Velocity = [0,0]
                
        elif (event.type == KEYDOWN):
            
            if (event.key == K_UP):
                actor.State = 'RUNNING'
                actor.Facing = 'BACK'
                actor.Velocity = [0,-actor.MovementSpeed]
        
            elif (event.key == K_DOWN):
                actor.State = 'RUNNING'
                actor.Facing = 'FRONT'
                actor.Velocity = [0,actor.MovementSpeed]
            elif (event.key == K_LEFT):
                actor.State = 'RUNNING'
                actor.Facing = 'LEFT'
                actor.Velocity = [-actor.MovementSpeed,0]
        
            elif (event.key == K_RIGHT):
                actor.State = 'RUNNING'
                actor.Facing = 'RIGHT'
                actor.Velocity = [actor.MovementSpeed,0]
                
            elif (event.key == K_SPACE):
                actor.State = 'ELEC'
                
#returns velocity!
def GhostTrackTo(actor, destination, min_distance):
  
    distx = actor.Position[0] - destination[0]
    disty = actor.Position[1] - destination[1]
    absdistx = abs(distx)
    absdisty = abs(disty)


    if absdistx <= min_distance and absdisty <= min_distance:
        actor.Velocity = [0,0]
    
    elif absdistx <= min_distance and absdisty > min_distance:
        if disty > 0:
            actor.Velocity = [0,-1*actor.MovementSpeed]
        else:
            actor.Velocity = [0,actor.MovementSpeed]

    elif absdistx > min_distance and absdisty <= min_distance:
        if distx > 0:
            actor.Velocity = [-1*actor.MovementSpeed,0]
        else:
            actor.Velocity = [actor.MovementSpeed,0]
        
 
    else:
        vel = [0,0]
        if distx > 0: vel[0] = -1*actor.MovementSpeed
        else: vel[0] = actor.MovementSpeed
        if distx > 0: vel[1] = -1*actor.MovementSpeed
        else: vel[1] = actor.MovementSpeed
        actor.Velocity = vel
            
    # close in in the dimension 